<?php
if (!function_exists('get_category_url')){
	function get_category_url($category_name = '', $category_id = 0, $total = 0){
		$CI =& get_instance();
		
		$url =  current_url();
		$new_cat = $category_id . '-' . string_to_url($category_name);
		$segment_array = $CI->uri->segment_array();
		if (isset($segment_array[4]) && $segment_array[2] == 'product_list'){
			if ($segment_array[4] == $new_cat){
				$segment_array[4] = 0;
			} else {
				$segment_array[4] = $new_cat;
			}
		} else {
			if (count($segment_array) > 2){
				foreach ($segment_array as $key=>$value){
					if ($key > 2){
						unset($segment_array[$key]);
					}
				}
			}
			$segment_array[2] = 'product_list';
			$segment_array[] = 0;
			$segment_array[] = $new_cat;		
		}
		
		if ($total) {
			return join('/', $segment_array);	
		}
		else {
			return base_url() . join('/', $segment_array);
		}
		
	}
}
if (!function_exists('get_product_url')){
	function get_product_url($prod_name = '',$prod_id = 0){
		$CI =& get_instance();
		
		$url =  current_url();
		$new_prod = $prod_id . '-' . string_to_url($prod_name);
		$segment_array = $CI->uri->segment_array();
		
		$final = array();
		
		$final[] = $segment_array[1];		
		$final[] = "product";
		$final[] = "get";
		$final[] = $new_prod;
			
		return base_url() . join('/', $final);
		
	}
}
if (!function_exists('get_filter_url')){
	function get_filter_url($filter_name = '', $filter_value = '', $total = 0){
		$CI =& get_instance(); 
		
		$new_value = urlencode($filter_value);
		$segment_array = $CI->uri->segment_array();
		
		if (isset($segment_array[5]) && $segment_array[2] == 'product_list'){
			if (strpos($segment_array[5], $filter_name) !== FALSE){
				$segment_element = explode(";",$segment_array[5]);
				
				foreach ($segment_element as $idx=>$element){
					if (strpos($element, $filter_name) !== FALSE){
						if ($element == $filter_name . '=' . $new_value){
							unset($segment_element[$idx]);
						} else {
							$segment_element[$idx] = substr($element, 0, strpos($element, "="));
							$segment_element[$idx] .= '=' .$new_value;
						}
					}
				}
				
				$segment_array[5] = join(';', $segment_element);
				
			} else {
				$segment_array[5] .= ';' . $filter_name . "=" . $new_value;
			}
		} else {
			if(!isset($segment_array[2]) OR $segment_array[2] != 'product_list'){
				if (count($segment_array) > 2){
					foreach ($segment_array as $key=>$value){
						if ($key > 2){
							unset($segment_array[$key]);
						}
					}
				}
				$segment_array[2] = 'product_list';
				$segment_array[] = 0;
				$segment_array[] = 0;
			}
			$segment_array[] = $filter_name . "=" . $new_value;		
		}
		
		if ($total) {
			return join('/', $segment_array);	
		}
		else {
			return base_url() . join('/', $segment_array);
		}
	}
}

if (!function_exists('string_to_url')){
	function string_to_url($string){
		$string = preg_replace('/[^a-zA-Z0-9,.-\s]/', '', $string);
		//$search = array('Ă�', 'ĂŁ', 'Ă‚', 'Ă˘', 'ĂŽ', 'Ă®', 'ÂŞ', 'Âş', 'Ăž', 'Ăľ', 'ÂŞ', 'Âş', 'Ăž', 'Ăľ');
		$search = array("\xC4\x82", "\xC4\x83", "\xC3\x82", "\xC3\xA2", "\xC3\x8E", "\xC3\xAE", "\xC8\x98", "\xC8\x99", "\xC8\x9A", "\xC8\x9B", "\xC5\x9E", "\xC5\x9F", "\xC5\xA2", "\xC5\xA3");
		$replace = array('A', 'a', 'A', 'a', 'I', 'i', 'S', 's', 'T', 't', 'S', 's', 'T', 't');
		$out = strtolower(str_replace($search, $replace, $string));
		$search = array(" ","\"","'","â€ž","â€ť","(",")",":","?","!","\\","/","%");
		$replace = array('-','','','','','','','','','','','','');
		$out = strtolower(str_replace($search, $replace, $out));
		return $out;
	}
}